<?php
/**
 * Created by PhpStorm.
 * User: EZ
 * Date: 2017/6/27
 * Time: 16:45
 */
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;
class Base extends Controller{
    public $admin_id=null;
    public $admin_info=null;
    public  static $controller=null;
    public  static $action=null;
    public  static $auth=null;

   public function __construct()
   {
       parent::__construct();
       $this->isLogin();
       $this->setController();
       $this->setAction();
       $this->setAuth();
       $this->checkAuth();
   }

    public function setController(){
        if(self::$controller ==null){
            self::$controller=Request::instance()->controller();
        }
    }
    public function setAction(){
        if(self::$action ==null){
            self::$action=Request::instance()->action();
        }
    }

    public function setAuth(){
        if(self::$auth=== null){
            $auth=session('admin_info')['auth'];
            if($auth !='all'){
                self::$auth=unserialize(base64_decode($auth));
            }
            if($auth=='all'){
                self::$auth='all';
            }
        }
    }

    public function  menu(){

        $auth=session('admin_info')['auth'];
        if($auth !='all'){
            $auth=unserialize(base64_decode($auth));
        }

       $menu= db('menu')->where("display=1")->select();


       $ar=[];
        foreach($menu as $k=>$v){
            $ar[$v['id']]=$v;
            $ar[$v['id']]['url']=U($v['module_name']."/".$v['action_name']);
        };
        $arr=[];
        foreach($ar as $k=>$v){
            $ar[$k]['list']=[];
            if(isset($ar[$v['pid']])){
                $arr[$v['pid']]['list'][]=&$ar[$k];
            }else{
                $arr[$v['id']]=&$ar[$k];
            }
        }
        return $arr;
    }

    public function isLogin(){
        $this->admin_id=session('admin_id');
        if(!($this->admin_id>0)){
             $this->redirect(U('login/login'));
        }
        $auth=session('admin_info')['auth'];
        if($auth !='all'){
            $this->auth=unserialize(base64_decode($auth));
        }
        if($auth=='all'){
            $this->$auth='all';
        }

    }

    public function send($method,$data=array()){
        $client= \Hprose\Client::create("http://test.7cai.tv/index.php/api/api/user",false);
        $client->setHeader('Authorization',$data['sig']);
        $res = $client->{$method}($data);
        return $res;
    }

    //获取userInfo 信息接口 参数 $sig=['sig'=>'sdfsdfsgsd4s4f4sd']
    public function getUserInfo($sig=array()){
        $result=$this->send('getUserInfo',$sig);
        $res=json_decode($result,true);
        return $res;
    }
    //修改七彩币数量接口 参数 score:增减的七彩币数量 type:增减的七彩币类型 0=减 1=增 sig
    public function modifyScore($score,$type,$sig){
        $data['score']=$score;
        $data['type']=$type;
        $data['sig']=$sig;
        $result=$this->send('modifyScore',$data);
        $res=json_decode($result,true);
        return $result;
    }
    //获取支付订单信息 amount:支付金额 type:支付 subject:商品标题
    public function getPayOrderInfo($amount,$type,$subject,$sig){
        $data['amount']=$amount;
        $data['type']=$type;
        $data['subject']=$subject;
        $data['sig']=$sig;
        $result=$this->send('getPayOrderInfo',$data);
        $res=json_decode($result,true);
        return $result;
    }

    //权限验证
    public function checkAuth(){
        $where=[
            'module_name'=>strtolower(self::$controller),
            'action_name'=>self::$action
        ];
        $ar=db('menu')->field('id')->where($where)->find();
        $ar=Db::connect()->table('menu')->field('id')->where($where)->find();
        $ar=Db::table('menu')->field('id')->where($where)->find();
        $id=isset($ar['id'])? $ar['id']:0;
        if(self::$auth=='all') return true;
        if(is_array(self::$auth) && in_array($id,self::$auth)){
            return true;
        }
        $this->error('无权限');
    }

}